package cn.fourhundredandeighteen.controller;

import cn.fourhundredandeighteen.entity.Student;
import cn.fourhundredandeighteen.entity.Subject;
import cn.fourhundredandeighteen.service.SubjectService;
import cn.fourhundredandeighteen.service.SubjectStudentService;
import cn.fourhundredandeighteen.service.impl.SubjectServiceImpl;
import cn.fourhundredandeighteen.service.impl.SubjectStudentServiceImpl;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.utill.ResultGenerator;

import java.util.List;

/**
 * @description:比赛信息操作
 * @author: liuyongqi
 * @time: 2020/12/19
 */
public class SubjectController {
    /**获奖信息录入，
     * 获奖信息录入完成后还需录入获奖成员id
     * @param subject
     * @return 保存后的id
     * 功能已添加
     */
    public Result setSubject(Subject subject){
        SubjectService service=new SubjectServiceImpl();
        try {
            Long i = service.saveSubject(subject);
            if (i>0)
                return ResultGenerator.successResult(i);
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }
    }

    /**获奖信息写入参赛成员id
     * @param subjectId 获奖名单id
     * @param studentIds 参赛学生id
     * @return 是否保存成功
     * 功能已添加
     */
    public Result setSubjectUsers(Long subjectId,Long[] studentIds){
        SubjectStudentService subjectStudentService=new SubjectStudentServiceImpl();
        try {
            int i = subjectStudentService.insertStudentAndSubjectList(subjectId, studentIds);
            if (i>0)
                return ResultGenerator.successResult(i);
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }

    /**
     * 精确获奖信息查询
     * @param subjectId
     * @return
     * 功能已添加
     */
    public Result<Subject> getSubjectOne(Long subjectId){
        SubjectService subjectService=new SubjectServiceImpl();
        try {
            Subject subjectInfo = subjectService.getSubjectInfo(subjectId);
            if (subjectInfo==null)
                return ResultGenerator.errorResult();
            return ResultGenerator.successResult(subjectInfo);
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }
    }
    /** 查询全部奖项信息
     * @return 全部奖项
     * 功能已添加
     */
    public Result<List<Subject>> getAllSubjectInfo(){
        SubjectService subjectService=new SubjectServiceImpl();
        try {
            List<Subject> all = subjectService.getAll();
            return ResultGenerator.successResult(all);
        }catch (Exception e){
            return ResultGenerator.errorResult();
        }

    }

    /** 获奖信息修改,必须有id在
     * @param subject
     * @return
     * 功能已添加
     */
    public Result upDataSubject(Subject subject){
        SubjectService subjectService=new SubjectServiceImpl();
        int i = subjectService.upDataSubject(subject);
        if (i>0)
            return ResultGenerator.successResult();
        else
            return ResultGenerator.errorResult();
    }

    /**获奖信息删除
     * @param subjectId
     * @return
     * 功能已添加
     */
    public Result delSubject(Long subjectId){
        SubjectService service=new SubjectServiceImpl();
        try {
            int i = service.delSubject(subjectId);
            if (i>0)
                return ResultGenerator.successResult();
            else
                return ResultGenerator.errorResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }
    }

    /**奖项的学生信息查询
     * @param subjectId 奖项id
     * @return 学生信息
     */
    public Result<List<Student>> getSubjectUsers(Long subjectId){
        SubjectService service=new SubjectServiceImpl();
        try {
            List<Student> studentInfo = service.getStudentInfo(subjectId);
            if (studentInfo==null)
                return ResultGenerator.errorResult();
            return ResultGenerator.successResult(studentInfo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.errorResult();
        }

    }
}
